import axios from 'axios';

const baseURL = 'http://localhost:3000';


// 获取所有商品
const getProducts = async () => {
    try {
        const response = await axios.get(`${baseURL}/products`);
        return response.data;
    } catch (error) {
        console.error('获取商品失败:', error);
        throw error;
    }
};

// 添加商品
const addProduct = async (product) => {
    try {
        const response = await axios.post(`${baseURL}/products`, product);
        return response.data;
    } catch (error) {
        console.error('添加商品失败:', error);
        throw error;
    }
};

// 更新商品
const updateProduct = async (product) => {
    try {
        const response = await axios.put(`${baseURL}/products/${product.id}`, product);
        return response.data;
    } catch (error) {
        console.error('更新商品失败:', error);
        throw error;
    }
};

// 删除商品
const deleteProduct = async (productId) => {
    try {
        await axios.delete(`${baseURL}/products/${productId}`);
        return true;
    } catch (error) {
        console.error('删除商品失败:', error);
        throw error;
    }
};

// 获取所有订单
const getOrders = async () => {
    try {
        const response = await axios.get(`${baseURL}/orders`);
        return response.data;
    } catch (error) {
        console.error('获取订单失败:', error);
        throw error;
    }
};

// 添加订单
const addOrder = async (order) => {
    try {
        const response = await axios.post(`${baseURL}/orders`, order);
        return response.data;
    } catch (error) {
        console.error('添加订单失败:', error);
        throw error;
    }
};

// 更新订单
const updateOrder = async (order) => {
    try {
        const response = await axios.put(`${baseURL}/orders/${order.orderId}`, order);
        return response.data;
    } catch (error) {
        console.error('更新订单失败:', error);
        throw error;
    }
};

// 删除订单
const deleteOrder = async (orderId) => {
    try {
        await axios.delete(`${baseURL}/orders/${orderId}`);
        return true;
    } catch (error) {
        console.error('删除订单失败:', error);
        throw error;
    }
};

// 获取所有用户
const getUsers = async () => {
    try {
        const response = await axios.get(`${baseURL}/users`);
        return response.data;
    } catch (error) {
        console.error('获取用户失败:', error);
        throw error;
    }
};

// 添加用户
const addUser = async (user) => {
    try {
        const response = await axios.post(`${baseURL}/users`, user);
        return response.data;
    } catch (error) {
        console.error('添加用户失败:', error);
        throw error;
    }
};

// 更新用户
const updateUser = async (user) => {
    try {
        const response = await axios.put(`${baseURL}/users/${user.userId}`, user);
        return response.data;
    } catch (error) {
        console.error('更新用户失败:', error);
        throw error;
    }
};

// 删除用户
const deleteUser = async (userId) => {
    try {
        await axios.delete(`${baseURL}/users/${userId}`);
        return true;
    } catch (error) {
        console.error('删除用户失败:', error);
        throw error;
    }
};

// 获取所有分类
const getCategories = async () => {
    try {
        const response = await axios.get(`${baseURL}/categories`);
        return response.data;
    } catch (error) {
        console.error('获取分类失败:', error);
        throw error;
    }
};

// 添加分类
const addCategory = async (category) => {
    try {
        const response = await axios.post(`${baseURL}/categories`, category);
        return response.data;
    } catch (error) {
        console.error('添加分类失败:', error);
        throw error;
    }
};

// 更新分类
const updateCategory = async (category) => {
    try {
        const response = await axios.put(`${baseURL}/categories/${category.id}`, category);
        return response.data;
    } catch (error) {
        console.error('更新分类失败:', error);
        throw error;
    }
};

// 删除分类
const deleteCategory = async (categoryId) => {
    try {
        await axios.delete(`${baseURL}/categories/${categoryId}`);
        return true;
    } catch (error) {
        console.error('删除分类失败:', error);
        throw error;
    }
};

export {
    getProducts,
    addProduct,
    updateProduct,
    deleteProduct,
    getOrders,
    addOrder,
    updateOrder,
    deleteOrder,
    getUsers,
    addUser,
    updateUser,
    deleteUser,
    getCategories,
    addCategory,
    updateCategory,
    deleteCategory
};